package com.sample.xcrash.slice;

import com.sample.xcrash.ResourceTable;

import com.sample.xcrash_lib.XCrash;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

@SuppressWarnings("InfiniteLoopStatement")
public class MainAbilitySlice extends AbilitySlice {

    final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "button");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Button testNativeCrashInMainThreadButton = (Button) findComponentById(ResourceTable.Id_testNativeCrashInMainThreadButton);

        testNativeCrashInMainThreadButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button1");
                XCrash.testNativeCrash(false);
            }
        });

        Button testNativeCrashInAnotherJavaThreadButton = (Button) findComponentById(ResourceTable.Id_testNativeCrashInAnotherJavaThreadButton);

        testNativeCrashInAnotherJavaThreadButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button2");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        XCrash.testNativeCrash(false);
                    }
                }, "java_thread_with_a_very_long_name").start();
            }
        });

        Button testNativeCrashInAnotherNativeThreadButton = (Button) findComponentById(ResourceTable.Id_testNativeCrashInAnotherNativeThreadButton);

        testNativeCrashInAnotherNativeThreadButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button3");
                XCrash.testNativeCrash(true);
            }
        });

        Button testNativeCrashInAnotherActivityButton = (Button) findComponentById(ResourceTable.Id_testNativeCrashInAnotherActivityButton);

        testNativeCrashInAnotherActivityButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button4");
                // 跳转
                Intent secondIntent = new Intent();
                secondIntent.setParam("type", "native"); // 传参
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.sample.xcrash")
                        .withAbilityName("com.sample.xcrash.SecondAbility")
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            }
        });

        Button testNativeCrashInAnotherProcessButton = (Button) findComponentById(ResourceTable.Id_testNativeCrashInAnotherProcessButton);

        testNativeCrashInAnotherProcessButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button5");
                // 跳转
                Intent myService = new Intent();
                myService.setParam("type", "native"); // 传参
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.sample.xcrash")
                        .withAbilityName("com.sample.xcrash.MyService")
                        .build();
                myService.setOperation(operation);
                startAbility(myService);
                // 连接Service
                connectAbility(intent, connection);
            }
        });

        Button testJavaCrashInMainThreadButton = (Button) findComponentById(ResourceTable.Id_testJavaCrashInMainThreadButton);

        testJavaCrashInMainThreadButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button6");
                XCrash.testJavaCrash(false);
            }
        });

        Button testJavaCrashInAnotherThreadButton = (Button) findComponentById(ResourceTable.Id_testJavaCrashInAnotherThreadButton);

        testJavaCrashInAnotherThreadButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button7");
                XCrash.testJavaCrash(true);
            }
        });

        Button testJavaCrashInAnotherActivityButton = (Button) findComponentById(ResourceTable.Id_testJavaCrashInAnotherActivityButton);

        testJavaCrashInAnotherActivityButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button8");
                // 跳转
                Intent secondIntent = new Intent();
                secondIntent.setParam("type", "java"); // 传参
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.sample.xcrash")
                        .withAbilityName("com.sample.xcrash.SecondAbility")
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent);
            }
        });

        Button testJavaCrashInAnotherProcessButton = (Button) findComponentById(ResourceTable.Id_testJavaCrashInAnotherProcessButton);

        testJavaCrashInAnotherProcessButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button9");
                // 跳转
                Intent myService = new Intent();
                myService.setParam("type", "java"); // 传参
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.sample.xcrash")
                        .withAbilityName("com.sample.xcrash.MyService")
                        .build();
                myService.setOperation(operation);
                startAbility(myService);
            }
        });

        Button testAnrInputButton = (Button) findComponentById(ResourceTable.Id_testAnrInputButton);

        testAnrInputButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.error(label, "button10");
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ignored) {
                    }
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    // 创建连接回调实例
    private final IAbilityConnection connection = new IAbilityConnection() {
        // 连接到Service的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            // Client侧需要定义与Service侧相同的IRemoteObject实现类。开发者获取服务端传过来IRemoteObject对象，并从中解析出服务端传过来的信息。
        }

        // 断开与Service连接的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {

        }
    };
}
